/**
 * Created by caelan on 2018/10/26.
 * gis图形制作，请求行政区域
 */
var countryArea = {
  /*行政区本地数据全国三级联动2016年7月*/
  areaData: null,
  init: function (areaCode) {
    var _this = this;
    /*获取区域行政数据*/
    $.ajax({
      // url: '../assets/localData/countryArea.json',
      url: 'assets/localData/areas.json',
      type: 'get',
      dataType: 'json',
      success: function (res) {
        /*渲染dom*/
        _this.renderArea(res,areaCode)
      },
      error: function (e) {
        console.log(e);
      }
    });

    /*ui选择事件 省*/
    layForm.on('select(provinceArea)', function (data) {
      var cities = _this.areaData[data.value].child,
        option = '';
      for ( var i in cities ) {
        if ( cities.hasOwnProperty(i) )
          // option += '<option value="'+ i +'">'+ cities[i].name +'</option>'
          option += '<option value="'+ i +'">'+ cities[i] +'</option>'
      }
      $('#gisGraphics').find('select[name="cityArea"]').html(option);
      layForm.render(null, 'gisGraphics');
      $('select[name="cityArea"]').next().find('dd:eq(0)').trigger('click');
    });
  },
  renderArea: function (res,areaCode) {
    var _this = this;
    _this.areaData = res;
    var prov = '';
    if(areaCode.length==2){
      for ( var i in res ) {
        if ( res.hasOwnProperty(i) )
          prov += '<option value="'+ i +'">'+ res[i].name +'</option>';
      }
    }else if(areaCode.length==4){
      for ( var i in res ) {
        if(i!="province"){
          prov += '<option value="'+ i +'">'+ res[i].name +'</option>';
        }  
      }
    }else{
      for ( var i in res ) {
        if(i=="country"){
          if ( res.hasOwnProperty(i) )
          prov += '<option value="'+ i +'">'+ res[i].name +'</option>';
        }
      }
    }
    /*添加dom*/
    $('#gisGraphics').find('select[name="provinceArea"]').html(prov);
    /*ui组件渲染*/
    layForm.render(null, 'gisGraphics');
    /*触发模板选择*/
     $('select[name="provinceArea"]').next().find('dd:eq(0)').trigger('click');
  }
};